//
//  LJFriend.m
//  LJClientKit
//
//  Created by Anton Chebotaev on 9/11/09.
//  Copyright 2009 AntonChebotaev. All rights reserved.
//

#import "LJFriend.h"

// Exceptions
NSString * const LJFriendBadInitException = @"LJFriendBadInitException";


@implementation LJFriend

@synthesize username;
@synthesize fullname;
@synthesize type;

- (id)init {
	[self dealloc];
    @throw [NSException exceptionWithName:LJFriendBadInitException
								   reason:@"Initialize LJFriend with simple init without username and fullname"
								 userInfo:nil];
    return nil;	
}


- initWithUsername: (NSString *)aUsername fullName:(NSString *)aFullName type:(LJFriendType)aType {
	self = [super init];
	if (self) {
        username = [aUsername retain];
        fullname = [aFullName retain];
		type = aType;
    }
    return self;
}
+ friendWithUsername: (NSString *)aUsername fullName:(NSString *)aFullName type:(LJFriendType)aType {
	return [[[LJFriend alloc] initWithUsername:aUsername fullName:aFullName type:aType] autorelease];
}

- (id)initWithCoder:(NSCoder *)decoder {
	self = [super init];
    if (self) {
        username = [[decoder decodeObjectForKey:@"LJFriendCodingUsername"] retain];
        fullname = [[decoder decodeObjectForKey:@"LJFriendCodingFullname"] retain];
		type = [decoder decodeIntForKey:@"LJFriendCodingType"];
		
        NSAssert(username != nil, @"LJFriend decoded without an username");
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:username forKey:@"LJFriendCodingUsername"];
    [encoder encodeObject:fullname forKey:@"LJFriendCodingFullname"];
	[encoder encodeInt:type forKey:@"LJFriendCodingType"];	
}

- (NSString *)description {
	return [NSString stringWithFormat:@"%@/%@", username, fullname];
}

- (NSUInteger)hash {
	return [[self description] hash];
}

- (BOOL)isEqual:(id)object {
	return [object isMemberOfClass:[LJFriend class]] && [[self description] isEqual:[object description]];
}
@end
